<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <!--  <el-form-item label="学院编号" prop="collegeNumber">
                        <el-input v-model="form.collegeNumber" placeholder="请输入学院编号" />
                      </el-form-item>-->
      <el-form-item label="学院名称" prop="collegeName">
        <el-input v-model="form.collegeName" placeholder="请输入学院名称" />
      </el-form-item>
      <el-form-item label="学院代码" prop="collegeCode">
        <el-input v-model="form.collegeCode" placeholder="请输入学院代码" />
      </el-form-item>
      <!-- <el-form-item label="负责人" prop="userId">
                        <el-input v-model="form.userId" placeholder="请输入负责人" />
                      </el-form-item> -->
      <el-form-item label="负责人" prop="userId">
        <el-input v-model="form.userId" placeholder="请输入负责人" />
        <!-- <select-user v-model="form.userId"></select-user> -->
      </el-form-item>

      <el-form-item label="开办时间" prop="openingTime">
        <el-date-picker
          clearable
          v-model="form.openingTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择开办时间"
        />
      </el-form-item>
      <!-- <el-form-item label="机构编号" prop="orgId">
                        <el-input v-model="form.orgId" placeholder="请输入机构编号" />
                      </el-form-item> -->
      <el-form-item label="学院简介" prop="remark">
        <el-input
          type="textarea"
          :rows="2"
          v-model="form.remark"
          placeholder="请输入备注"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createColleges, updateColleges } from "@/api/collegebase/colleges";
import selectUser from '@/components/CollegeSelectUser/CollegeSelectUser.vue'
export default {
  name: "CollegesEdit",
  components: {
    selectUser
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      // 表单校验
      rules: {
        collegeName: [
          { required: true, message: '请输入学院名称', trigger: 'blur' }
        ],
        collegeCode: [
          { required: true, message: '请输入学院代码', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '请输入负责人', trigger: 'blur' }
        ],
        openingTime: [
          { required: true, message: '请选择开办时间', trigger: 'blur' }
        ],

      }
    };
  },
  created() { },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: ''
    }
  },
  methods: {
    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.datetimerange = [this.form.startTime, this.form.endTime]
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit('update:show', false)
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        collegeNumber: undefined,
        collegeName: undefined,
        collegeCode: undefined,
        userId: undefined,
        openingTime: undefined,
        orgId: undefined,
        remark: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateColleges(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit('success')
          });
          return;
        }
        // 添加的提交
        createColleges(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit('success')
        });
      });
    },

  }
};
</script>
